#include "queue.h"


RingBuff usart1_buff;

void RingBuffIni(RingBuff *pbuff)
{
    memset(pbuff, 0x00, sizeof(RingBuff));
}
uint8_t Read_RingBuff(RingBuff *pbuff, DataBuff *rdata)
{
    if (pbuff->lenght == 0)
        return 1;
    pbuff->lock = 1;
    *rdata = pbuff->data_buff[pbuff->head];
    pbuff->head = (pbuff->head + 1) % ringbuff_len;
    pbuff->lenght--;
    pbuff->lock = 0;
    return 0;
}
uint8_t Write_RingBuff(RingBuff *pbuff, uint8_t *wdata, uint16_t len)
{
    if ((pbuff->lenght >= ringbuff_len) || ( !len))
        return 1;
    while(pbuff->lock == 1) {}
    pbuff->data_buff[pbuff->tail].len = len;
    for(int i = 0; i < len; i++) {
        pbuff->data_buff[pbuff->tail].data[i] = wdata[i];
    }
    pbuff->tail = (pbuff->tail + 1) % ringbuff_len;
    pbuff->lenght++;
    return 0;

}
uint8_t RingBuffRst(RingBuff *pbuff)
{
    memset(pbuff, 0x00, sizeof(RingBuff));
    return 0;
}
